<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<meta name="keywords" content="Unit testing" />
<link rel="shortcut icon" href="/favicon.ico" />
<link rel="search" type="application/opensearchdescription+xml" href="/wikitissue/opensearch_desc.php" title="WikiTissue (English)" />
<link title="Creative Commons" type="application/rdf+xml" href="/wikitissue/index.php?title=Unit_testing&amp;action=creativecommons" rel="meta" />
<link rel="copyright" href="http://www.gnu.org/copyleft/fdl.html" />
		<title>Unit testing - WikiTissue</title>
		<style type="text/css" media="screen,projection">/*<![CDATA[*/ @import "/wikitissue/skins/monobook/main.css?42b"; /*]]>*/</style>
		<link rel="stylesheet" type="text/css" media="print" href="/wikitissue/skins/common/commonPrint.css?42b" />
		<link rel="stylesheet" type="text/css" media="handheld" href="/wikitissue/skins/monobook/handheld.css?42b" />
		<!--[if lt IE 5.5000]><style type="text/css">@import "/wikitissue/skins/monobook/IE50Fixes.css?42b";</style><![endif]-->
		<!--[if IE 5.5000]><style type="text/css">@import "/wikitissue/skins/monobook/IE55Fixes.css?42b";</style><![endif]-->
		<!--[if IE 6]><style type="text/css">@import "/wikitissue/skins/monobook/IE60Fixes.css?42b";</style><![endif]-->
		<!--[if IE 7]><style type="text/css">@import "/wikitissue/skins/monobook/IE70Fixes.css?42b";</style><![endif]-->
		<!--[if lt IE 7]><script type="text/javascript" src="/wikitissue/skins/common/IEFixes.js?42b"></script>
		<meta http-equiv="imagetoolbar" content="no" /><![endif]-->
		
		<script type= "text/javascript">/*<![CDATA[*/
var skin = "monobook";
var stylepath = "/wikitissue/skins";
var wgArticlePath = "/wikitissue/index.php/$1";
var wgScriptPath = "/wikitissue";
var wgServer = "http://www.opentissue.org";
var wgCanonicalNamespace = "";
var wgCanonicalSpecialPageName = false;
var wgNamespaceNumber = 0;
var wgPageName = "Unit_testing";
var wgTitle = "Unit testing";
var wgArticleId = "1467";
var wgIsArticle = true;
var wgUserName = null;
var wgUserLanguage = "en";
var wgContentLanguage = "en";
var wgBreakFrames = false;
var wgCurRevisionId = "1732";
/*]]>*/</script>
                
		<script type="text/javascript" src="/wikitissue/skins/common/wikibits.js?42b"><!-- wikibits js --></script>
		<script type="text/javascript" src="/wikitissue/index.php?title=-&amp;action=raw&amp;gen=js"><!-- site js --></script>
		<style type="text/css">/*<![CDATA[*/
@import "/wikitissue/index.php?title=MediaWiki:Common.css&usemsgcache=yes&action=raw&ctype=text/css&smaxage=18000";
@import "/wikitissue/index.php?title=MediaWiki:Monobook.css&usemsgcache=yes&action=raw&ctype=text/css&smaxage=18000";
@import "/wikitissue/index.php?title=-&action=raw&gen=css&maxage=18000";
/*]]>*/</style>
		<!-- Head Scripts -->
			</head>
<body  class="mediawiki ns-0 ltr page-Unit_testing">
	<div id="globalWrapper">
		<div id="column-content">
	<div id="content">
		<a name="top" id="top"></a>
				<h1 class="firstHeading">Unit testing</h1>
		<div id="bodyContent">
			<h3 id="siteSub">From WikiTissue</h3>
			<div id="contentSub"></div>
									<div id="jump-to-nav">Jump to: <a href="#column-one">navigation</a>, <a href="#searchInput">search</a></div>			<!-- start content -->
			<p>This little guide is a short introduction on how to get the Boost <a href="http://www.boost.org/libs/test/doc/components/utf/index.html" class="external text" title="http://www.boost.org/libs/test/doc/components/utf/index.html" rel="nofollow">link Unit Testing Framework</a> up and running. It consist of three steps
</p>
<ul>
<li>Make sure boost is installed correctly</li>
<li>Create a cpp unit test file</li>
<li>Setup linker properties</li>
</ul>
<p>The boost installment and linkker steps are specific for a windows environment running VC80. 
</p>
<table id="toc" class="toc" summary="Contents"><tr><td><div id="toctitle"><h2>Contents</h2></div>
<ul>
<li class="toclevel-1"><a href="#Is_Boost_installed_correctly.3F"><span class="tocnumber">1</span> <span class="toctext">Is Boost installed correctly?</span></a></li>
<li class="toclevel-1"><a href="#Creating_a_Test_.28Suite.29_Application"><span class="tocnumber">2</span> <span class="toctext">Creating a Test (Suite) Application</span></a></li>
<li class="toclevel-1"><a href="#Shutting_up_the_Linker"><span class="tocnumber">3</span> <span class="toctext">Shutting up the Linker</span></a></li>
<li class="toclevel-1"><a href="#How_to_organize_and_create_unit_tests_in_OpenTissue"><span class="tocnumber">4</span> <span class="toctext">How to organize and create unit tests in OpenTissue</span></a></li>
</ul>
</td></tr></table><script type="text/javascript"> if (window.showTocToggle) { var tocShowText = "show"; var tocHideText = "hide"; showTocToggle(); } </script>
<a name="Is_Boost_installed_correctly.3F"></a><h2><span class="editsection">[<a href="/wikitissue/index.php?title=Unit_testing&amp;action=edit&amp;section=1" title="Edit section: Is Boost installed correctly?">edit</a>]</span> <span class="mw-headline"> Is Boost installed correctly?</span></h2>
<p>More details can be found at the boost <a href="http://www.boost.org/more/getting_started.html" class="external text" title="http://www.boost.org/more/getting_started.html" rel="nofollow">link getting started</a> home page. An easy way of doing some of the steps below might be to use a <a href="http://www.boost-consulting.com/products/free/beta" class="external text" title="http://www.boost-consulting.com/products/free/beta" rel="nofollow">link installer</a> that takes care of all the copying and binaries. When I (kenny) install the boost library on my windows machine running VC80, I perform the following steps
</p>
<ol>
<li>Download latest boost version</li>
<li>Unpack boost to whatever location one likes</li>
<li>Create an environment variable named %BOOST% pointing to the unpacked location</li>
<li>Build bjam (run build_dist.bat file in tools subfolder of the unpacked boost library), copy bjam.exe to whatever location one likes. I choice %BOOST%/</li>
<li>Change working folder to the boost location</li>
<li>Run:
<pre>
bjam --build_dir=%TMP% --toolset=msvc-8.0 stage
</pre>
</li>
<li>copy %BOOSt%/stage/libs to %BOOST%/libs</li>
<li>Delete the folders %BOOST%/stage and %BOOST%/bin.v2</li>
<li>Install the boost-bindings, simply by unpacking the bindings into the same location as one where one have unpacked boost.</li>
</ol>
<p>One do not need the bindings in order for using the boost unit testing framework. Actually one do not need to create boost binaries these can be created along side ones test application (see more about this below) and the added cost of longer compilation times.
</p><p>The BOOST environment variable is very important, because this is how OpenTissue gets all its information about where it is supposed to find all the boost stuff that is used.
</p><p>A minimal setup would be to simply unpack boost and setup an environment variable, but if one wants every part of OpenTissue to work and reduce compile times, then all of the steps above must be done.
</p>
<a name="Creating_a_Test_.28Suite.29_Application"></a><h2><span class="editsection">[<a href="/wikitissue/index.php?title=Unit_testing&amp;action=edit&amp;section=2" title="Edit section: Creating a Test (Suite) Application">edit</a>]</span> <span class="mw-headline"> Creating a Test (Suite) Application</span></h2>
<p>When using unit tests, create a cpp-file and write
</p>
<pre>
#include &lt;boost/test/unit_test.hpp&gt;
using boost::unit_test::test_suite;

void some_test_function()
{
    BOOST_CHECK(&nbsp;??? );
}

test_suite*
init_unit_test_suite( int, char* [] ) {
    test_suite* test= BOOST_TEST_SUITE( &quot;Unit test example&quot; );

    test-&gt;add( BOOST_TEST_CASE( &amp;some_test_function ) );

    return test;
}
</pre>
<p>This is in a sense a minimal unit test program. If one needs more advanced testing then please refer to the boost documentation on the <a href="http://www.boost.org/libs/test/doc/components/utf/index.html" class="external text" title="http://www.boost.org/libs/test/doc/components/utf/index.html" rel="nofollow">link Unit Testing Framework</a>.
</p>
<a name="Shutting_up_the_Linker"></a><h2><span class="editsection">[<a href="/wikitissue/index.php?title=Unit_testing&amp;action=edit&amp;section=3" title="Edit section: Shutting up the Linker">edit</a>]</span> <span class="mw-headline"> Shutting up the Linker</span></h2>
<p>Next open the: 
</p>
<center>project properties</center>
<p>and in the link tab page under the item
</p>
<center>Additional Library Directories</center>
<p>add
</p>
<center>$(BOOST)\lib</center>
<p>VC80 will automatically figure out what lib-file that is needed,
so one does not have to do any more.
</p><p><b>Optional:</b> If one gets into lib-file linker hell and do not mind
longer compile times then one can simply add the header file inlcude
</p>
<pre>
#include  &lt;boost/test/included/unit_test_framework.hpp&gt;
</pre>
<p>to ones cpp-file above. Then the boost unit test framework will get compiled
along side with ones test suite.
</p><p><br />
</p>
<a name="How_to_organize_and_create_unit_tests_in_OpenTissue"></a><h1><span class="editsection">[<a href="/wikitissue/index.php?title=Unit_testing&amp;action=edit&amp;section=4" title="Edit section: How to organize and create unit tests in OpenTissue">edit</a>]</span> <span class="mw-headline">How to organize and create unit tests in OpenTissue</span></h1>
<p>Thanks to Joergen Havsberg Seland for the inspiration to set up unit testing in OpenTissue. Some of the bits and pieces in below are more or less copied directly from Joergen's description on the 3Dot wiki (anno May 2007). With a few added OpenTissue tweaks made by myself (kenny).
</p>
<center><b>WARNING:</b> Only compatible with Boost 1_34_0</center>
<p>Unit tests should be placed into the OpenTissue unit test folder, which is located at 
</p>
<pre>
$(OPENTISSUE)/units
</pre>
<p>The structure inside the unit-test folder is a flat-atructure. Each subfolder contains an independent unit test. The folder name of the folder containing the test should be pre-fixed with unit_ and the remaining part of the name should reflect what the unit is actually testing. For instance say one wants to create unit tests for the stuff in the basic_math_types.h header file in OpenTissue. Then one would create a unit tes t folder like
</p>
<pre>
$(OPENTISSUE)/units/unit_basic_math_types/
</pre>
<p>Now one can simply create cpp-files for doing the actual unit-testing inside a src-folder also CMakelists.txt files should be added. For the above example this could look like
</p>
<pre>
$(OPENTISSUE)/units/unit_basic_math_types/src/unit_basic_math_types.cpp
$(OPENTISSUE)/units/unit_basic_math_types/CMakelists.txt
</pre>
<p>Rember to add your new unit-test to the CMakelist.txt file in the folder $(OPENTISSUE)/units/.
</p><p>All unit tests should be put in a top level test suite named with the namespace and name of the class or file being tested, separated by underscore (depending on whether the test subject is a class or just a group of functions). An example cpp-file is shown below.
</p>
<pre>
//
// OpenTissue, A toolbox for physical based simulation and animation.
// Copyright (C) 2007 Department of Computer Science, University of Copenhagen
//
#include &lt;OpenTissue/configuration.h&gt;

// Add Include headers from the code being tested
#include &lt;OpenTissue/math/basic_math_types.h&gt;

#define BOOST_AUTO_TEST_MAIN
#include &lt;boost/test/auto_unit_test.hpp&gt;

// Boost Test declaration and Checking macros
#include &lt;boost/test/unit_test_suite.hpp&gt;
#include &lt;boost/test/test_tools.hpp&gt;

BOOST_AUTO_TEST_SUITE(opentisue_math_basic_math_types);  

	BOOST_AUTO_TEST_SUITE();

		BOOST_AUTO_TEST_CASE(my_first_test_case)
		{
			BOOST_CHECK_EQUAL( ...., true);
			BOOST_CHECK_EQUAL( ...., true);
		}

		BOOST_AUTO_TEST_CASE(my_second_test_case)
		{
			BOOST_CHECK_EQUAL( ...., true);
			BOOST_CHECK_EQUAL( ...., true);
		}

		BOOST_AUTO_TEST_CASE(my_third_test_case)
		{
			BOOST_CHECK_EQUAL( ...., true);
			BOOST_CHECK_EQUAL( ...., true);
		}

	BOOST_AUTO_TEST_SUITE_END();

	BOOST_AUTO_TEST_CASE(my_allways_fail_test_case)
	{
		BOOST_CHECK(false);
	}

BOOST_AUTO_TEST_SUITE_END();
</pre>
<p>Important points about this code, are:
</p>
<ol>
<li>Because we are using the boost::unit_test automatic test registration facility, we do not need a header file for the tests, and we do not need to write any extra code for registering them. We just make sure the cpp file containing the tests is included in the project, and the tests will be available.</li>
<li>You may freely nest test suites within each other.</li>
</ol>
<p>See the <a href="http://www.boost.org/libs/test/doc/components/utf/index.html" class="external text" title="http://www.boost.org/libs/test/doc/components/utf/index.html" rel="nofollow">link boost test library</a> for more information on writing unit tests. 
</p><p>Exposing the tests is handled automatically. Behind the scenes, the following stuff happens:
</p>
<ol>
<li>The BOOST_AUTO_TEST_CASE and BOOST_AUTO_TEST_SUITE macros create static initializer objects.</li>
<li>When the shared library or executable is loaded, the initializer objects are constructed, and register your test suites and test cases with the boost::unit_test::framework, in the boost-unit_test shared library. Note that this means that all test cases in all loaded libraries (all extensions etc.) are gathered in one place automatically.</li>
</ol>

<!-- Saved in parser cache with key wikitissuedb-wikitissue_:pcache:idhash:1467-0!1!0!!en!2 and timestamp 20070816035538 -->
<div class="printfooter">
Retrieved from "<a href="http://www.opentissue.org/wikitissue/index.php/Unit_testing">http://www.opentissue.org/wikitissue/index.php/Unit_testing</a>"</div>
						<!-- end content -->
			<div class="visualClear"></div>
		</div>
	</div>
		</div>
		<div id="column-one">
	<div id="p-cactions" class="portlet">
		<h5>Views</h5>
		<div class="pBody">
			<ul>
						 <li id="ca-nstab-main" class="selected"><a href="/wikitissue/index.php/Unit_testing">Article</a></li>
						 <li id="ca-talk" class="new"><a href="/wikitissue/index.php?title=Talk:Unit_testing&amp;action=edit">Discussion</a></li>
						 <li id="ca-edit"><a href="/wikitissue/index.php?title=Unit_testing&amp;action=edit">Edit</a></li>
						 <li id="ca-history"><a href="/wikitissue/index.php?title=Unit_testing&amp;action=history">History</a></li>
				</ul>
		</div>
	</div>
	<div class="portlet" id="p-personal">
		<h5>Personal tools</h5>
		<div class="pBody">
			<ul>
				<li id="pt-login"><a href="/wikitissue/index.php?title=Special:Userlogin&amp;returnto=Unit_testing">Log in / create account</a></li>
			</ul>
		</div>
	</div>
	<div class="portlet" id="p-logo">
		<a style="background-image: url(/wikitissue/logo_64.gif);" href="/wikitissue/index.php/Main_Page" title="Main Page"></a>
	</div>
	<script type="text/javascript"> if (window.isMSIE55) fixalpha(); </script>
		<div class='portlet' id='p-navigation'>
		<h5>Navigation</h5>
		<div class='pBody'>
			<ul>
				<li id="n-mainpage"><a href="/wikitissue/index.php/Main_Page">Main Page</a></li>
				<li id="n-portal"><a href="/wikitissue/index.php/WikiTissue:Community_Portal">Community portal</a></li>
				<li id="n-currentevents"><a href="/wikitissue/index.php/Current_events">Current events</a></li>
				<li id="n-recentchanges"><a href="/wikitissue/index.php/Special:Recentchanges">Recent changes</a></li>
				<li id="n-randompage"><a href="/wikitissue/index.php/Special:Random">Random page</a></li>
				<li id="n-help"><a href="/wikitissue/index.php/Help:Contents">Help</a></li>
				<li id="n-sitesupport"><a href="/wikitissue/index.php/WikiTissue:Site_support">Donations</a></li>
			</ul>
		</div>
	</div>
		<div id="p-search" class="portlet">
		<h5><label for="searchInput">Search</label></h5>
		<div id="searchBody" class="pBody">
			<form action="/wikitissue/index.php/Special:Search" id="searchform"><div>
				<input id="searchInput" name="search" type="text" accesskey="f" value="" />
				<input type='submit' name="go" class="searchButton" id="searchGoButton"	value="Go" />&nbsp;
				<input type='submit' name="fulltext" class="searchButton" id="mw-searchButton" value="Search" />
			</div></form>
		</div>
	</div>
	<div class="portlet" id="p-tb">
		<h5>Toolbox</h5>
		<div class="pBody">
			<ul>
				<li id="t-whatlinkshere"><a href="/wikitissue/index.php/Special:Whatlinkshere/Unit_testing">What links here</a></li>
				<li id="t-recentchangeslinked"><a href="/wikitissue/index.php/Special:Recentchangeslinked/Unit_testing">Related changes</a></li>
<li id="t-upload"><a href="/wikitissue/index.php/Special:Upload">Upload file</a></li>
<li id="t-specialpages"><a href="/wikitissue/index.php/Special:Specialpages">Special pages</a></li>
				<li id="t-print"><a href="/wikitissue/index.php?title=Unit_testing&amp;printable=yes">Printable version</a></li>				<li id="t-permalink"><a href="/wikitissue/index.php?title=Unit_testing&amp;oldid=1732">Permanent link</a></li>			</ul>
		</div>
	</div>
		</div><!-- end of the left (by default at least) column -->
			<div class="visualClear"></div>
			<div id="footer">
				<div id="f-poweredbyico"><a href="http://www.mediawiki.org/"><img src="/wikitissue/skins/common/images/poweredby_mediawiki_88x31.png" alt="Powered by MediaWiki" /></a></div>
				<div id="f-copyrightico"><a href="http://www.gnu.org/copyleft/fdl.html"><img src="/wikitissue/skins/common/images/gnu-fdl.png" alt='GNU Free Documentation License 1.2' /></a></div>
			<ul id="f-list">
				<li id="lastmod"> This page was last modified 09:20, 21 May 2007.</li>
				<li id="viewcount">This page has been accessed 245 times.</li>
				<li id="copyright">Content is available under <a href="http://www.gnu.org/copyleft/fdl.html" class="external " title="http://www.gnu.org/copyleft/fdl.html" rel="nofollow">GNU Free Documentation License 1.2</a>.</li>
				<li id="privacy"><a href="/wikitissue/index.php/WikiTissue:Privacy_policy" title="WikiTissue:Privacy policy">Privacy policy</a></li>
				<li id="about"><a href="/wikitissue/index.php/WikiTissue:About" title="WikiTissue:About">About WikiTissue</a></li>
				<li id="disclaimer"><a href="/wikitissue/index.php/WikiTissue:General_disclaimer" title="WikiTissue:General disclaimer">Disclaimers</a></li>
			</ul>
		</div>
		
	
		<script type="text/javascript">if (window.runOnloadHook) runOnloadHook();</script>
</div>
<!-- Served by www.opentissue.org in 0.244 secs. --></body></html>
